package com.sgj.controller.admin;

import com.sgj.pojo.Blog;
import com.sgj.pojo.User;
import com.sgj.service.BlogService;
import com.sgj.service.TagService;
import com.sgj.service.TypeService;
import com.sgj.vo.BlogRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @author 沈国纪
 * @date 2021/3/30 14:25
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

	private static final String BLOGS_INPUT = "admin/blogs-input";
	private static final String BLOGS = "admin/blogs";
	private static final String REDIRECT_BLOGS = "redirect:/admin/blogs";

	@Autowired
	private BlogService blogService;

	@Autowired
	private TypeService typeService;

	@Autowired
	private TagService tagService;

	/**
	 * 分页查询
	 * @param pageable
	 * @param blogRes
	 * @param model
	 * @return
	 */
	@GetMapping("/blogs")
	public String blogs(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
	                    BlogRes blogRes, Model model) {
		model.addAttribute("types", typeService.listType());
		model.addAttribute("page", blogService.listBlog(pageable, blogRes));
		return BLOGS;
	}

	/**
	 * 博客搜索
	 * @param pageable
	 * @param blogRes
	 * @param model
	 * @return
	 */
	@PostMapping("/blogs/search")
	public String search(@PageableDefault(size = 10, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
	                     BlogRes blogRes, Model model) {
		model.addAttribute("page", blogService.listBlog(pageable, blogRes));
		return "admin/blogs :: blogList";
	}

	/**
	 * 新增页面跳转
	 * @param model
	 * @return
	 */
	@GetMapping("/blogs/input")
	public String input(Model model) {
		setTypeAndTag(model);
		model.addAttribute("blog", new Blog());
		return BLOGS_INPUT;
	}

	/**
	 * 初始化标签和分类
	 * @param model
	 */
	private void setTypeAndTag(Model model) {
		model.addAttribute("types", typeService.listType());
		model.addAttribute("tags", tagService.listTag());
	}

	/**
	 * 修改博客
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/blogs/{id}/input")
	public String update(@PathVariable Long id, Model model) {
		setTypeAndTag(model);
		Blog blog = blogService.getBlog(id);
		blog.init();
		model.addAttribute("blog", blog);
		return BLOGS_INPUT;
	}

	/**
	 * 新增博客
	 * @param blog
	 * @param attributes
	 * @param session
	 * @return
	 */
	@PostMapping("/blogs")
	public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {
		blog.setUser((User) session.getAttribute("user"));
		blog.setType(typeService.findTypeById(blog.getType().getId()));
		blog.setTags(tagService.listTag(blog.getTagIds()));
		Blog b;
		if (blog.getId() == null) {
			b = blogService.saveBlog(blog);
		} else {
			b = blogService.updateBlog(blog);
		}
		if (Objects.isNull(b)) {
			attributes.addFlashAttribute("message", "操作失败");
		} else {
			attributes.addFlashAttribute("message", "操作成功");
		}
		return REDIRECT_BLOGS;
	}

	/**
	 * 删除博客
	 * @param id
	 * @param attributes
	 * @return
	 */
	@GetMapping("/blogs/{id}/delete")
	public String delete(@PathVariable Long id, RedirectAttributes attributes) {
		blogService.deleteBlog(id);
		attributes.addFlashAttribute("message", "删除成功");
		return REDIRECT_BLOGS;
	}
}
